import Sprite from '../base/sprite'
import util from './util'

const screenWidth = window.innerWidth
const screenHeight = window.innerHeight

export default class rankPanel{
  constructor(name, panel, arrow){
    let l = panel.left
    this.rank_list = new Sprite("images/astray/buttons.png", screenWidth * (1 - 2 * l), screenHeight * panel.height, l * screenWidth, screenHeight * panel.top, 2, 
    {sx: 36, sy: 276, sw: 184, sh: 170}, name + "_panel")
    //this.rank_list.force_update = true
    this.previous_stage = new Sprite("images/astray/left.png", screenWidth * l, screenHeight * arrow.height, l * screenWidth, screenHeight * arrow.top, 3, null, name + "_previous")
    this.next_stage = new Sprite("images/astray/right.png", screenWidth * l, screenHeight * arrow.height, screenWidth * (1 - 2 * l), screenHeight * arrow.top, 3, null, name + "_next")
    this.rank_updated = true
  }

  enterSwitch3D(currentStage, minStage, maxStage){
    this.current_stage = currentStage
    this.min_stage = minStage
    this.max_stage = maxStage
    this.rank_updated = false
    if (this.current_stage < this.min_stage)
      this.current_stage = this.min_stage
  }

  controlSwitch3D(x, y){
    if (this.next_stage.pointIsIn(x, y)){
      if (this.current_stage < this.max_stage){
        this.current_stage += 1
        this.rank_updated = false
      }
    }else if (this.previous_stage.pointIsIn(x, y)){
      if (this.current_stage> this.min_stage){
        this.current_stage -= 1
        this.rank_updated = false
      }
    }
  }

  async renderSwitch3D(ctx, txts){
    if (!this.rank_updated){
      this.rank_list.customUpdate({sx: 36, sy: 276, sw: 184, sh: 170, texts: txts})
      if (this.current_stage == this.max_stage)
        this.next_stage.clear(ctx)
      else
        this.next_stage.visible = true
      if (this.current_stage == this.min_stage)
        this.previous_stage.clear(ctx)
      else
        this.previous_stage.visible = true
      this.rank_updated = true
    }
    let fs = this.rank_list.force_update
    await this.rank_list.drawToCanvas(ctx, true)
    if (this.current_stage < this.max_stage){
      if (fs){
        this.next_stage.force_update = fs
      }
      this.next_stage.drawToCanvas(ctx)
    }
    if (this.current_stage > this.min_stage){
      if (fs)
        this.previous_stage.force_update = fs
      this.previous_stage.drawToCanvas(ctx)
    }
  }
}